package com.arrogames.rgblabyrinth.game.vision {
	import com.arrogames.lib.ash.rendering.Position;
	import com.arrogames.rgblabyrinth.game.targeting.PlayerNode;
	
	import ash.core.Engine;
	import ash.core.NodeList;
	import ash.core.System;
	
	import starling.display.Image;
	
	public class VisionSystem extends System {
		
		private var vision:Image;
		private var tileWidth:Number;
		
		private var player:PlayerNode;
		private var visibleTilesLength:Number = 20; //will come from number of shards eventually.
		
		public function VisionSystem(tileWidth:Number) {
			super();
			this.vision = vision;
			this.tileWidth = tileWidth;
		}
		
		override public function addToEngine(engine:Engine):void {
			super.addToEngine(engine);
			
			var players:NodeList = engine.getNodeList(PlayerNode);
			player = players.head;
			players.nodeAdded.add(onPlayerAdded);
			
		}
		
		private function onPlayerAdded(node:PlayerNode):void {
			this.player = node;
		}
		
		override public function update(time:Number):void {
			super.update(time);
			
			if (hasPlayer()) {
				var pos:Position = player.entity.get(Position) as Position;
				
				vision.width = visibleTilesLength*tileWidth;
				vision.height = visibleTilesLength*tileWidth;
				vision.x = pos.x-(vision.width/2);
				vision.y = pos.y-(vision.height/2);
			}
		}
		
		private function hasPlayer():Boolean {
			return player !== null;
		}
		
	}
}